package org.leopard.rpc.rmi;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Set;
import org.leopard.rpc.Consumer;
import org.leopard.rpc.ServiceCenter;

/**
 * @author IBM CHEN ZHI KAN
 * @create 2018/8/13
 */
public class RMIConsumer<T> extends Consumer{

  /**
   * 调用rmi远程服务
   * @return
   */
  public T lookupService(){
    Set<String> urls=ServiceCenter.getInstance("localhost").addres.get(this.getUrl());
    Object[] urlArray=urls.toArray();
    if(urlArray.length>0){
      try {
        return (T) Naming.lookup(String.valueOf(urlArray[0]));
      } catch (NotBoundException e) {
        e.printStackTrace();
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (RemoteException e) {
        e.printStackTrace();
      }
    }
    return null;
  }
}
